﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.XSchEmA.Entity;
using Pixysoft.Framework.XSchEmA;
using System.Data;
using Pixysoft.Tools;
using Pixysoft.Framework.Reflection;
using System.Xml.Serialization;

namespace Pixysoft.Framework.Noebe
{
    class quickstart
    {
        /// <summary>
        /// 创建数据库
        /// </summary>
        public void test_001()
        {
            //初始化数据库链接信息
            DatabaseInfo info = new DatabaseInfo();
            info.Filename = AppDomain.CurrentDomain.BaseDirectory + "\\sqlite.db";
            info.DatabaseType = DatabaseType.SQLite;

            DatabaseSchema db = new DatabaseSchema(info);
            TableSchema table = new TableSchema("HELLO");
            table.Columns.Add(new ColumnSchema("HELLO", "COLUMN1", new DataTypeSchema("TEXT")));
            table.Columns.Add(new ColumnSchema("HELLO", "COLUMN2", new DataTypeSchema("TEXT")));
            table.Columns.Add(new ColumnSchema("HELLO", "COLUMN3", new DataTypeSchema("TEXT")));
            db.Tables.Add(table);
            db.PrimaryKeys.Add(new PrimaryKeySchema("HELLO_PK", "HELLO", "COLUMN1"));
            db.Build();

            //部署
            XSchemaManager.SQLiteSchema.Schema.Commit(info, db);
        }

        /// <summary>
        /// 增删改查
        /// </summary>
        public void test_002()
        {
            NoebeConfiguration config = new NoebeConfiguration();
            config.DatabaseType = DatabaseType.SQLite;
            config.Filename = AppDomain.CurrentDomain.BaseDirectory + "\\sqlite.db";
            NoebeManager.Reload(config);

            //插入
            DataTable table = NoebeManager.Instance.GetEntity("HELLO");
            DataRow row = table.NewRow();
            row["COLUMN1"] = GlobalTimer.Instance.GetGlobalTime().ToString();
            row["COLUMN2"] = "column";
            row["COLUMN3"] = DateTime.Now;
            table.Rows.Add(row);
            NoebeManager.Instance.NoebeCommand.Insert(table);

            //查询
            INoebeCommand command = NoebeManager.Instance.NoebeCommand;
            command.SQL = "SELECT * FROM HELLO WHERE COLUMN2 = :COLUMN2";
            command.Parameters.Add("COLUMN2", "column");
            DataTable searchtable = command.ExecuteReader();
            Console.WriteLine(searchtable.Rows[0][0].ToString());

            //更新
            searchtable.Rows[0][2] = "column3";
            NoebeManager.Instance.NoebeCommand.Update(searchtable);
            Console.WriteLine(NoebeManager.Instance.NoebeCommand.Select("HELLO").Rows.Count);

            //删除
            NoebeManager.Instance.NoebeCommand.Delete(searchtable);
            Console.WriteLine(NoebeManager.Instance.NoebeCommand.Select("HELLO").Rows.Count);
        }

        /// <summary>
        /// 测试简易的ORM
        /// </summary>
        public void test_003()
        {
            NoebeConfiguration config = new NoebeConfiguration();
            config.DatabaseType = DatabaseType.SQLite;
            config.Filename = AppDomain.CurrentDomain.BaseDirectory + "\\sqlite.db";
            NoebeManager.Reload(config);

            //插入
            QuickstartHello hello = ReflectionManager.CreatePojo<QuickstartHello>();
            hello.Column1 = GlobalTimer.Instance.GetGlobalTime().ToString();
            hello.Column2 = "column";
            hello.Column3 = "column";
            NoebeManager.Instance.EntityNoebeCommand.Insert(hello);

            //查询
            IEntityNoebeCommand command = NoebeManager.Instance.EntityNoebeCommand;
            command.SQL = "SELECT * FROM HELLO WHERE COLUMN2 = :COLUMN2";
            command.Parameters.Add("COLUMN2", "column");
            List<QuickstartHello> hellos = command.ExecuteReader<QuickstartHello>();
            Console.WriteLine(hellos[0].Column2);

        }

    }

    [XmlRoot("HELLO")]
    public interface QuickstartHello : IEntityBase
    {
        string Column1 { get;set;}
        string Column2 { get;set;}
        string Column3 { get;set;}
    }
}
